package com.carl.zlds.tool.common.result;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;

public class Result<T> {
    private int code;
    @JsonInclude(Include.NON_EMPTY)
    private String msg;
    @JsonInclude(Include.NON_EMPTY)
    private T data;

    public int getCode() {
        return this.code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return this.msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return this.data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public static <T> Result<T> success(T data) {
        return new Result(data);
    }

    public static <T> Result<T> success() {
        return new Result();
    }

    public static <T> Result<T> error(CodeMsg codeMsg) {
        return new Result(codeMsg);
    }

    private Result() {
    }

    private Result(T data) {
        this.data = data;
    }

    private Result(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    private Result(CodeMsg codeMsg) {
        if (codeMsg != null) {
            this.code = codeMsg.getCode();
            this.msg = codeMsg.getMsg();
        }

    }

    @Override
    public String toString() {
        if (this.msg != null && !this.msg.equals("")) {
            return "Result{code=" + this.code + ", msg='" + this.msg + '}';
        } else {
            return this.data != null ? "Result{code=" + this.code + ", data=" + this.data + '}' : "Result{code=" + this.code + '}';
        }
    }
}
